package com.qfedu.ssm.controllers;

import com.qfedu.ssm.dao.UserDAO;
import com.qfedu.ssm.pojos.ItemVO;
import com.qfedu.ssm.pojos.User;
import com.qfedu.ssm.services.ItemService;
import com.qfedu.ssm.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class MController {
    @Autowired
    private UserService userService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private UserDAO userDAO;

    @RequestMapping("/login.do")
    public String login(HttpServletRequest request,String userName, String userPwd, Model model){
        try {
            userService.login(userName,userPwd);
            System.out.println("---success");
            User user = userDAO.queryUserByName(userName);
            request.getSession().setAttribute("user",user);
            List<ItemVO> list = itemService.listItems(user.getId());
            System.out.println(list);
            model.addAttribute("items",list);
            return "index.jsp";
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("---fail");
            model.addAttribute("tips","登录验证失败，请重试！");
            return "/login.jsp";
        }
    }
}
